#!/usr/bin/python
# -*- coding: utf-8 -*-
# vimtips.py -- parse tips from www.vim.org
# written by Ali Polatel

import os
import sys
import re, urllib2
import commands

__author__ = 'Ali Polatel'
__contributors__ = []

# Globals
URL = 'http://www.vim.org/tips/tip.php?tip_id=%d'
headers= { 'User-Agent': 'vimtips.py' }
href_r = re.compile(r'<a.*?href=["\'](?P<link>.*?)["\']>(?P<text>.*?)</a>',re.IGNORECASE)
             


class comment:
    """ Type for additional comments
    """
    email = None
    date = None
    text = None

def htmltowiki(str):
    """ Take care of html stuffed 
        that's not recognized by wikipedia
    """
    # Remove ^M's
    str = str.replace('\r','')
    
    # Convert href tags
    #m = href_r.findall(str)
    #for tag in m:
        # Add www.vim.org if the link starts with /
        # TODO change this to the corresponding wiki adress of the tip ;)
    #    if tag[0].startswith('/'):
    #        link = 'http://www.vim.org' + tag[0]
    #    str = re.sub(r'<a.*?href=.*?>%s</a>' % tag[1]
    #                 ,'[%s %s]' % (link, tag[1])
    #                 ,str)
    
    # Initial support for html2wiki
    # Very ugly but it does the job well
    fp = open('vimtip_tmp.html','w')
    fp.write(str)
    fp.close()
    status,tmpstr = commands.getstatusoutput('html2wiki --dialect MediaWiki vimtip_tmp.html')
    if status != 0: # html2wiki is not in PATH or it exited with error
        sys.stderr.write('html2wiki not found, skipping conversion\n')
    else:
        str= tmpstr
    os.remove('vimtip_tmp.html')
    return str

def gettip(id):
    """ Get the tip from www.vim.org
    """
    tip_url = URL % id    
    req = urllib2.Request(tip_url, None, headers)
    data = urllib2.urlopen(req)
    return data.read()

def parsetip(tip):
    """ Parse the tip
    """
    
    title_r = re.compile('(Tip #\d.*?)\s:')
    rating_r = re.compile('Rating\s<b>(-?\d+/\d+)')
    body_r = re.compile('<p><code>(.*)</code></p>')
    add_r = re.compile('<td class="lightbg">\n\t\s*\t\s*(?P<email>.*?),\s\n\t\s*\t\s*<.*>\s(?P<date>.*?)</font>')
    comment_r = re.compile('<td><tt>(.*)')

    try:
        title = title_r.search(tip).groups()[0]
    except AttributeError: # Tip doesn't exist
        return None
    rating = rating_r.search(tip).groups()[0]
    body = body_r.search(tip).groups()[0]
    
    # Additional comments
    comments = []
    m = add_r.finditer(tip)
    for match in m:
        new = comment()
        new.email = htmltowiki(match.group('email'))
        new.date = match.group('date')
        comments.append(new)
    
    m = comment_r.finditer(tip)
    index = 0
    for match in m:
        comments[index].text = htmltowiki(match.groups()[0])
        index+=1

    lst = tip.split('\n')
    
    # The lines we want are two lines below these lines
    created_td = '    <td class="prompt">created:</td>'
    complexity_td = '    <td class="prompt">complexity:</td>'
    author_td = '    <td class="prompt">author:</td>'
    version_td = '    <td class="prompt">as of Vim:</td>'

    created = re.sub('\s*<.?td>','',lst[lst.index(created_td)+2])
    complexity = re.sub('\s*<.?td>','',lst[lst.index(complexity_td)+2])
    author = re.sub('\s*<.?td>','',lst[lst.index(author_td)+2])
    version = re.sub('\s*<.?td>','',lst[lst.index(version_td)+2])
    
    if not version: # Version empty
        version = 'n/a'

    return htmltowiki(title), author, created, complexity, version, rating, htmltowiki(body), comments

if __name__ == '__main__':

    if len(sys.argv) != 2:
        print 'Usage %s tip_id' % sys.argv[0]
        sys.exit(-1) 
    id = int(sys.argv[1])
    tip = gettip(id)
    parsedtip = parsetip(tip)
   
    if parsedtip is None:
        print 'No tip with id %d exists on www.vim.org' % id
        sys.exit(-2)
    
    # Produces text with basic wikipedia syntax.. feel free to edit
    print '== ', parsedtip[0], ' =='
    print '{| class="wikitable" style="text-align:center" border="0"'
    print '|-'
    print '! author'
    print '| \'\'', parsedtip[1], '\'\''
    print '|-'
    print '! created'
    print '| ', parsedtip[2]
    print '|-'
    print '! complexity'
    print '| ', parsedtip[3]
    print '|-'
    print '! as of Vim'
    print '| ', parsedtip[4]
    print '|-'
    print '! rating'
    print '| ', parsedtip[5]
    print '|}'
    print 
    print parsedtip[6]
    print 
    # And additional comments
    print '=== Additional Notes ==='
    print 
    comment = parsedtip[7]
    index = 0
    while index < len(comment):
        #print '==== Note %d ====' % index
        print '{| class="wikitable" style="text-align:center" border="0"'
        print '|-'
        print "! By"
        print "| ", comment[index].email
        print "! On"
        print "| ", comment[index].date
        print '|-'
        print '|}'
        print
        print comment[index].text
        index +=1
    print '<!-- parsed by vimtips.py :-) -->' 
